package com.blog.admin.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.blog.admin.pojo.auth.User;

public class UserUtils {

    /**
     * 获取当前登录的用户，若用户未登录，则返回未登录 json
     *
     * @return
     */
    public static User currentLoginUser() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            Object principal = subject.getPrincipals().getPrimaryPrincipal();
            if (principal instanceof User) {
                return (User) principal;
            }
        }
        return null;
    }
    
    /**
     * 验证手机号码格式
     * @param str
     * @return
     */
	public static boolean isTelphone(String str) {
		String regEx = "^((13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8])|(19[0-9])|)\\d{8}$";// "[1]"代表第1位为数字1，"[358]"代表第二位可以为3、5、8中的一个，"\\d{9}"代表后面是可以是0～9的数字，有9位。 
		Pattern pattern = Pattern.compile(regEx);
	    Matcher matcher = pattern.matcher(str);
	    // 字符串是否与正则表达式相匹配
	    boolean rs = matcher.matches();
	    return rs;
	}
	
	/**
     * 验证用户是否是admin管理员
     * @return
     */
	public static boolean isAdmin() {
		Subject subject = SecurityUtils.getSubject();
        if (subject.hasRole("ADMIN")) {
            return true;
        }
        return false;
	}
}
